<?php
	class productModel extends MY_Model
	{
		function __construct()
		{
			// Call the Model constructor
			parent::__construct();
			$this->table = "product";
			$this->tableLan = "productLan";
			$this->table_id = "product_id";		
		}
		
		function get_front_product($lan, $where)
		{			
			$sql = "select m.*, lan.*, (select category_url from categoryLan c where c.category_id = m.category_id AND lan = '$lan') as category_url from product m inner join " . $this->tableLan . " lan on `m`.`" . $this->table_id . "` = `lan`.`".$this->table_id."` where lan = '$lan' AND $where";
			$query = $this->db->query($sql);
			return $query->result();
		}
		
		function count_search($keyword)
		{
			$this->add_search_data($keyword);
			return $this->db->count_all_results($this->table);				
		}
		
		function search($keyword, $lan, $paging_value = 0, $start = 0)
		{
			$this->add_search_data($keyword);
			if(empty($paging_value))
				return $this->db->get_where($this->table, array("lan" => $lan))->result();
			else
				return $this->db->get_where($this->table, array("lan" => $lan), $paging_value, $start)->result();
		}
		
		function add_search_data($keyword)
		{
			$keyword = mysql_real_escape_string($keyword);
			$where = "(`productName` like '%{$keyword}%' OR `productShortDesc` like '%{$keyword}%' OR `productDesc` like '%{$keyword}%') AND `visible` = 'y'";
			$this->db->join($this->tableLan, "{$this->table}.{$this->table}_id = {$this->tableLan}.{$this->table}_id", 'left');
			$this->db->where($where);			
		}
	}
?>